package com.lepu.app.usercenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.app.utils.DspTongJi;
import com.app.utils.UmengEvent;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.core.AsyncRequest;
import com.core.lib.utils.main.AppManager;
import com.core.lib.utils.main.UmengHelper;
import com.core.lib.widget.MyDialog;
import com.eyzhs.app.R;
import com.lepu.app.usercenter.bean.BeanPwdQuestion;
import com.lepu.app.usercenter.util.UserCenterUtil;
import com.lepu.app.widget.CustomTopBarNew;
import com.lepu.app.widget.CustomTopBarNew.OnTopbarNewLeftLayoutListener;

public class RegisterByQuestionActivity extends BaseFragmentActivity implements
		OnTopbarNewLeftLayoutListener, AsyncRequest {

	private static final String REQUEST_REGISTER = "request_register";
	private static final String REQUEST_PWD_QUESTION = "request_pwd_question";

	private static final int MSG_REGISTER_SUCCESS = 10;
	private static final int MSG_REGISTER_FAIL = 11;

	private static final int MSG_PWD_QUESTION_SUCCESS = 20;
	private static final int MSG_PWD_QUESTION_FAIL = 21;

	private String mPhone = "";
	private ArrayList<BeanPwdQuestion> mDataList = null;
	private String[] mStringArray = null;

	private int mQuestionIndex0 = 0;
	private int mQuestionIndex1 = 0;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.usercenter_register_by_question_activity);

		init();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	/**
	 * 添加页面动画，需要重写这个方法，
	 */
	@Override
	public void onBackPressed() {
		finish(true);
	}

	/**
	 * 初始化方法
	 */
	private void init() {

		// topbar
		CustomTopBarNew topbar = (CustomTopBarNew) findViewById(R.id.topbar);
		topbar.setTopbarTitle("密保注册");
		topbar.setonTopbarNewLeftLayoutListener(this);

		Bundle data = getIntent().getExtras();
		if (data != null && data.size() > 0) {
			mPhone = data.getString("phone");
		}

		TextView phoneTextView = (TextView) findViewById(R.id.phoneTextView);
		phoneTextView.setText(mPhone);

		RelativeLayout questionLayout1 = (RelativeLayout) findViewById(R.id.questionLayout1);
		RelativeLayout questionLayout2 = (RelativeLayout) findViewById(R.id.questionLayout2);
		Button okButton = (Button) findViewById(R.id.okButton);

		questionLayout1.setOnClickListener(mOnClickListener);
		questionLayout2.setOnClickListener(mOnClickListener);
		okButton.setOnClickListener(mOnClickListener);

		requestQuestions();
	}

	private void requestRegister() {

		// TextView questionTextView1 = (TextView)
		// findViewById(R.id.questionTextView1);
		EditText questionEditText1 = (EditText) findViewById(R.id.questionEditText1);

		// TextView questionTextView2 = (TextView)
		// findViewById(R.id.questionTextView2);
		EditText questionEditText2 = (EditText) findViewById(R.id.questionEditText2);

		EditText pwdEditText = (EditText) findViewById(R.id.pwdEditText);
		EditText pwdEditText2 = (EditText) findViewById(R.id.pwdEditText2);

		// 1
		// String question1 = questionTextView1.getText().toString().trim();
		String question1Content = questionEditText1.getText().toString().trim();
		// String question2 = questionTextView2.getText().toString().trim();
		String question2Content = questionEditText2.getText().toString().trim();

		String pwdContent = pwdEditText.getText().toString().trim();
		String pwdContent2 = pwdEditText2.getText().toString().trim();

		boolean flag = UserCenterUtil.register(this, this, REQUEST_REGISTER,
				true, mPhone, "-1", pwdContent, pwdContent2,"","",
				mDataList.get(mQuestionIndex0).SQID, question1Content,
				mDataList.get(mQuestionIndex1).SQID, question2Content,0);
		if (flag) {
			showProgressDialog();
		}
	}

	private void requestQuestions() {
		boolean flag = UserCenterUtil.getPwdQuestion(this, this,
				REQUEST_PWD_QUESTION);
		if (flag) {
			showProgressDialog();
		}
	}

	private void updateQuestionText(final int index) {
		if (mDataList != null && mDataList.size() > 0) {

			final int totalCount = mDataList.size();
			// int totalCount = 13;//test
			mStringArray = null;

			int count = 0;
			if (index == 0) {
				count = totalCount / 2;
				mStringArray = new String[count];

				for (int i = 0; i < count; i++) {
					mStringArray[i] = mDataList.get(i).Question;
				}
			} else {
				int startCount = totalCount / 2;
				count = totalCount - startCount;
				mStringArray = new String[count];

				for (int i = 0; i < count; i++) {
					mStringArray[i] = mDataList.get(i + startCount).Question;
				}
			}

			new MyDialog(RegisterByQuestionActivity.this)
					.setSimpleItems(mStringArray,
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

									String question = mStringArray[which];
									if (index == 0) {
										mQuestionIndex0 = which;

										TextView questionTextView1 = (TextView) findViewById(R.id.questionTextView1);
										questionTextView1.setText(question);
									} else {
										int startCount = totalCount / 2;
										mQuestionIndex1 = which + startCount;

										TextView questionTextView2 = (TextView) findViewById(R.id.questionTextView2);
										questionTextView2.setText(question);
									}
								}
							}).create(null).show();

		} else {
			requestQuestions();
		}
	}

	View.OnClickListener mOnClickListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.questionLayout1:

				updateQuestionText(0);
				break;
			case R.id.questionLayout2:

				updateQuestionText(1);
				break;
			case R.id.okButton:

				requestRegister();
				break;
			default:
				break;
			}
		}
	};

	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler() {
		public void handleMessage(Message msg) {

			switch (msg.what) {
			case MSG_REGISTER_SUCCESS:
				hideProgressDialog();

				// 注册成功后，统计注册用户

				// 只需要dsp渠道,统计注册
				new DspTongJi()
						.requestRegisterSuccess(RegisterByQuestionActivity.this);

				AppManager.getAppManager().finishActivity(
						RegisterActivity.class);
				finish(true);
				break;
			case MSG_REGISTER_FAIL:
				hideProgressDialog();
				break;
			case MSG_PWD_QUESTION_SUCCESS:
				hideProgressDialog();
				break;
			case MSG_PWD_QUESTION_FAIL:
				hideProgressDialog();
				break;

			default:
				break;
			}
		};
	};

	@Override
	public void onTopbarLeftLayoutSelected() {
		finish(true);
	}

	@Override
	public void RequestComplete(Object object, Object data) {
		if (object.equals(REQUEST_REGISTER)) {
			String result = (String) data;
			int code = 0;
			try {
				JSONObject jsonObject = new JSONObject(result);
				JSONObject obj1 = (JSONObject) jsonObject.get("Result");
				code = obj1.optInt("Status");
			} catch (JSONException e) {
				e.printStackTrace();
			}

			if (code == 200) {
				mHandler.sendEmptyMessage(MSG_REGISTER_SUCCESS);
			} else {
				mHandler.sendEmptyMessage(MSG_REGISTER_FAIL);
			}
		} else if (object.equals(REQUEST_PWD_QUESTION)) {

			mDataList = BeanPwdQuestion.parseJsonData((String) data);

			if (mDataList == null) {
				mDataList = new ArrayList<BeanPwdQuestion>();
			}
			mHandler.sendEmptyMessage(MSG_PWD_QUESTION_SUCCESS);
		}
	}

	@Override
	public void RequestError(Object object, int errorId, String errorMessage) {
		if (object.equals(REQUEST_REGISTER)) {
			mHandler.sendEmptyMessage(MSG_REGISTER_FAIL);
		} else if (object.equals(REQUEST_PWD_QUESTION)) {
			mHandler.sendEmptyMessage(MSG_PWD_QUESTION_FAIL);
		}
	}

}
